package com.dycn.posdashboard.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.dycn.posdashboard.data.Branch2MerchantMapping;
import com.dycn.posdashboard.data.JsonResponseData;
import com.dycn.posdashboard.data.MerchantInfo;
import com.dycn.posdashboard.data.ServicesResultObject;
import com.dycn.posdashboard.data.User;
import com.dycn.posdashboard.dbhelper.DataBaseUser;
import com.dycn.posdashboard.listener.SystemInitListener;
import com.dycn.posdashboard.services.PosTradeServices;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
    protected final Log log = LogFactory.getLog(getClass());
    /**
     * <pre>
     * 
     * </pre>
     */
    private static final long serialVersionUID = 1L;

    private User         ue;
    private JsonResponseData<User> jsonResponseData;
//    private LoginService loginService;
    private PosTradeServices posTradeServices;
    
    private JsonResponseData<Object> commonResponseDataJson;
    
    
    public JsonResponseData<Object> getCommonResponseDataJson() {
		return commonResponseDataJson;
	}
	public void setCommonResponseDataJson(
			JsonResponseData<Object> commonResponseDataJson) {
		this.commonResponseDataJson = commonResponseDataJson;
	}
	public PosTradeServices getPosTradeServices() {
		return posTradeServices;
	}
	public void setPosTradeServices(PosTradeServices posTradeServices) {
		this.posTradeServices = posTradeServices;
	}
//	public LoginService getLoginService() {
//		return loginService;
//	}
//	public void setLoginService(LoginService loginService) {
//		this.loginService = loginService;
//	}
	public JsonResponseData<User> getJsonResponseData() {
		return jsonResponseData;
	}
	public void setJsonResponseData(JsonResponseData<User> jsonResponseData) {
		this.jsonResponseData = jsonResponseData;
	}

    public User getUe() {
        return ue;
    }
    public void setUe(User ue) {
        this.ue = ue;
    }
    
    /**
     * 
     * @return
     */
	public String loginDemo() {
		commonResponseDataJson=new JsonResponseData<Object>();
		commonResponseDataJson.setSuccess(false);
		commonResponseDataJson.setResultCode(JsonResponseData.LOGIC_ERROR);
		commonResponseDataJson.setMsg("参数提交错误~~~");
		commonResponseDataJson.setData(null);
      
    	HttpServletRequest request = ServletActionContext.getRequest();  
    	String loginChannel=request.getParameter("loginSystemType");//登录channel  0->分公司 1-商户 2->线下用户 3 线上用户
    	
    	String loginUserName=request.getParameter("userName");
    	String loginPwd=request.getParameter("password");
    	
    	if(loginChannel==null||loginChannel.trim().equals("")){
    		loginChannel="0";
    	}
    	
    	if(loginUserName==null||loginUserName.trim().equals("")
    			||loginPwd==null||loginPwd.trim().equals("")
    			||!loginPwd.equals(loginPwd)){
    		commonResponseDataJson.setSuccess(false);
    		commonResponseDataJson.setResultCode(JsonResponseData.LOGIC_ERROR);
    		commonResponseDataJson.setMsg("用户名或密码错误！");
    		commonResponseDataJson.setData(null);
    	}else{
    		ServicesResultObject<Branch2MerchantMapping> servicesResultObjectBranch2MerchantMapping=this.getPosTradeServices().getBranch2MerchantMappingInfo(DataBaseUser.MAIN_USER);
    		Branch2MerchantMapping resultBranch2MerchantMapping=servicesResultObjectBranch2MerchantMapping.getReturnObj();
    		
        	if(loginChannel.equals("0")){
        		List<MerchantInfo> matchedMerchantInfoList=resultBranch2MerchantMapping.getMerchantInfoByBranchNo(loginUserName);
        		if(matchedMerchantInfoList!=null&&matchedMerchantInfoList.size()!=0){
        			if(loginUserName.equals(loginPwd)){
            			commonResponseDataJson.setSuccess(true);
            			commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
            			commonResponseDataJson.setMsg("登录成功!!!");
            			Map<String,Object> resultData=new HashMap<String,Object>();
            			resultData.put("liveUpIntervale", SystemInitListener.LIVE_CHART_INTERVAL);
            			resultData.put("userName", loginUserName);
            			commonResponseDataJson.setData(resultData);
        			}else{
        	    		commonResponseDataJson.setSuccess(false);
        	    		commonResponseDataJson.setResultCode(JsonResponseData.LOGIC_ERROR);
        	    		commonResponseDataJson.setMsg("用户名或密码错误！");
        	    		commonResponseDataJson.setData(null);
        			}

        		}
        	}else if(loginChannel.equals("1")){
        		MerchantInfo matchedMerchantInfo=resultBranch2MerchantMapping.getMerchantInfoByMerchantNo(loginUserName);
        		if(matchedMerchantInfo!=null){
        			if(loginUserName.equals(loginPwd)){
            			commonResponseDataJson.setSuccess(true);
            			commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
            			commonResponseDataJson.setMsg("登录成功!!!");
            			Map<String,Object> resultData=new HashMap<String,Object>();
            			resultData.put("liveUpIntervale", SystemInitListener.LIVE_CHART_INTERVAL);
            			resultData.put("userName", loginUserName);
            			commonResponseDataJson.setData(resultData);
        			}else{
        	    		commonResponseDataJson.setSuccess(false);
        	    		commonResponseDataJson.setResultCode(JsonResponseData.LOGIC_ERROR);
        	    		commonResponseDataJson.setMsg("用户名或密码错误！");
        	    		commonResponseDataJson.setData(null);
        			}

        		} 
        	}else if(loginChannel.equals("2")||loginChannel.equals("3")){
    			if(loginUserName.equals(loginPwd)){
        			commonResponseDataJson.setSuccess(true);
        			commonResponseDataJson.setResultCode(JsonResponseData.SUCCESS);
        			commonResponseDataJson.setMsg("登录成功!!!");
        			Map<String,Object> resultData=new HashMap<String,Object>();
        			resultData.put("liveUpIntervale", SystemInitListener.LIVE_CHART_INTERVAL);
        			resultData.put("userName", loginUserName);
        			commonResponseDataJson.setData(resultData);
    			}else{
    	    		commonResponseDataJson.setSuccess(false);
    	    		commonResponseDataJson.setResultCode(JsonResponseData.LOGIC_ERROR);
    	    		commonResponseDataJson.setMsg("用户名或密码错误！");
    	    		commonResponseDataJson.setData(null);
    			}
        	}

    	}

		return SUCCESS;
	}
	
//	public String login() {
//		jsonResponseData=new JsonResponseData<User>();
//		jsonResponseData.setSuccess(false);
//        jsonResponseData.setResultCode(JsonResponseData.LOGIC_ERROR);
//        jsonResponseData.setMsg("参数提交错误~~~");
//        jsonResponseData.setData(null);
//        
//    	log.debug("action enh login thread id:"+Thread.currentThread().getId());
//    	log.debug("action enh login getUserName:"+ue.getUserName());
//    	log.debug("action enh login password:"+ue.getPassword());
//    	log.debug("action enh login getObscurePassword:"+ue.getObscurePassword());
//        if (ue != null ) {
//        	//if Admin and 12345678
//            if (ue.getUserName().equals("Admin")&&ue.getObscurePassword().equals("25d55ad283aa400af464c76d713c07ad")) {
//                //获取用户对应的dbuser
//            	//Integer res = (Integer) loginService.checkUser(DataBaseUser.MAIN_USER, ue);
//            	User u=new User();
//            	u.setDbUser("METADATA_USER");
//            	u.setUserName("Admin");
//                Map<String, Object> session = ActionContext.getContext().getSession();
//                session.put(LoginInterceptor.LOGIN_KEY, ue.getUserName());
//                session.put("DbUser", "METADATA_USER");
//                session.put("LoginUserInfor", u);
//                //JSONObject jsonObject = JSONObject.fromObject(u,JSONObject.fromObject();
//                
//                jsonResponseData.setSuccess(true);
//                jsonResponseData.setResultCode(JsonResponseData.SUCCESS);
//                jsonResponseData.setMsg("登录成功!!!");
//                jsonResponseData.setData(u);
//                return SUCCESS;
//            } else {
//
//            	Integer res = (Integer) loginService.checkUser(DataBaseUser.MAIN_USER, ue);
//            	if (res != null && res == 1) {
//                     //获取用户对应的dbuser
//                     User u = loginService.findUserInfoByUserName(DataBaseUser.MAIN_USER, ue);
//                     Map<String, Object> session = ActionContext.getContext().getSession();
//                     session.put(LoginInterceptor.LOGIN_KEY, ue.getUserName());
//                     session.put("DbUser", u.getDbUser());
//            	 
//                     jsonResponseData.setSuccess(true);
//                     jsonResponseData.setResultCode(JsonResponseData.SUCCESS);
//                     jsonResponseData.setMsg("登录成功!!!");
//                     jsonResponseData.setData(u);
//                     return SUCCESS;
//                     
//            	 } else {
//
//                     jsonResponseData.setSuccess(false);
//                     jsonResponseData.setResultCode(JsonResponseData.LOGIC_ERROR);
//                     jsonResponseData.setMsg("用户名或密码错误或没有开通权限！");
//                     jsonResponseData.setData(null);
//                     return SUCCESS;
//                 }
//            }
//            
//        	
//        	 
//        	
//        }
//        
//        return SUCCESS;
//    }

    public String logOut() {
		jsonResponseData=new JsonResponseData<User>();
		
        ActionContext.getContext().getSession().clear();
        ServletActionContext.getRequest().getSession().invalidate();
        
        jsonResponseData.setSuccess(true);
        jsonResponseData.setResultCode(JsonResponseData.SUCCESS);
        jsonResponseData.setMsg("");
        jsonResponseData.setData(null);
        
        return SUCCESS;
    }



   
}
